﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tokenization;
using Utilities;
using System.Xml.Serialization;

namespace WordDemo2
{
    public partial class Tokenization : Form
    {
        Tokenizer tk = new Tokenizer();
        CheckSpell cs;
        HashSet<string> listProcessed;
        Thread t;
        int maxim, x = 1;
        public Tokenization()
        {
            InitializeComponent();
            Debugger.createLog("Tokenization");
            Debugger.showLog(true);
            if (File.Exists("listProcessed.xml"))
                deseriable();
            else
                listProcessed = new HashSet<string>();
        }

        private void serizable()
        {
            Debugger.log("Seriable...");
            XmlSerializer xml = new XmlSerializer(typeof(HashSet<string>));
            TextWriter WriteFileStream = new StreamWriter("listProcessed.xml");
            xml.Serialize(WriteFileStream, listProcessed);
        }

        private void deseriable()
        {
            Debugger.log("Deseriable...");
            XmlSerializer xml = new XmlSerializer(typeof(HashSet<string>));
            FileStream ReadFileStream = new FileStream("listProcessed.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
            listProcessed = (HashSet<string>)xml.Deserialize(ReadFileStream);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            richTextBox2.Text = tk.Process(richTextBox1.Text);
            richTextBox1.Text = richTextBox2.Text;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = fbd.SelectedPath;
                DirectoryInfo di = new DirectoryInfo(fbd.SelectedPath);
                cs = new CheckSpell(fbd.SelectedPath);
                DataTable dt = new DataTable();
                dt.Columns.Add("name");
                dt.Columns.Add("path");
                FileInfo[] paths = di.GetFiles("*.xml");
                foreach (var i in paths)
                {
                    DataRow dr = dt.NewRow();
                    dr["name"] = i.Name;
                    dr["path"] = i.FullName;
                    dt.Rows.Add(dr);
                    tk.loadRules(i.FullName);
                }
                listPath.DataSource = dt;
            }
        }

        private void btnCreateRules_Click(object sender, EventArgs e)
        {
            tk.createRules(textBox1.Text);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            progressBar1.Maximum = 100;
            t = new Thread(() => { process(); });
            t.Start();
        }

        public void process()
        {
            DirectoryInfo din = new DirectoryInfo(textBox2.Text);
            DirectoryInfo dout = new DirectoryInfo(textBox3.Text);
            if (!dout.Exists) dout.Create();
            FileInfo[] files = din.GetFiles("*.txt", SearchOption.AllDirectories);
            maxim = files.Length;
            foreach (FileInfo f in files)
            {
                if (!listProcessed.Contains(f.FullName))
                {
                    Debugger.drawProgressBar(f + " is processing", x, maxim, 50, '#');
                    //progressBar1.Invoke((Action)(() => progressBar1.Value = (x * 100 / maxim)));
                    // progressBar1.CreateGraphics().DrawString(x.ToString() + "/" + maxim.ToString(), new Font("Arial",
                    //(float)10.25, FontStyle.Regular),
                    //Brushes.Black, new PointF(progressBar1.Width / 2 - 10, progressBar1.Height / 2 - 7));
                    x++;
                    string content = f.OpenText().ReadToEnd();
                    content = cs.Replace_CheckSpell(content);
                    content = tk.Process(content);

                    string path = f.FullName;
                    path = path.Replace(din.FullName, dout.FullName);
                    DirectoryInfo dii = new FileInfo(path).Directory;
                    if (!dii.Exists) dii.Create();
                    StreamWriter sw = new StreamWriter(path);
                    sw.Write(content);
                    sw.Close();
                    listProcessed.Add(f.FullName);
                }
            }
            Debugger.log("Xong");
        }

        private void Tokenization_FormClosed(object sender, FormClosedEventArgs e)
        {
            serizable();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            serizable();
            if (t.IsAlive)
            {
                t.Abort();
                Debugger.log("Stop thread");
            }
        }

        private void Tokenization_FormClosing(object sender, FormClosingEventArgs e)
        {

        }
    }
}
